package net.dfrz.oa.service.impl;

import java.util.Date;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.dfrz.oa.dao.repository.WorkLogRepository;
import net.dfrz.oa.pojo.User;
import net.dfrz.oa.pojo.WorkLog;
import net.dfrz.oa.service.WorkLogService;
import net.dfrz.oa.utils.PageBean;

@Service
@Transactional
public class WorkLogServiceImpl implements WorkLogService {

	@Autowired
	private WorkLogRepository workLogRepository;

	/**
	 * 查询当前用户的工作日志
	 */
	@Override
	@Transactional(readOnly=true)
	public Page<WorkLog> queryPageList(PageBean<WorkLog> pageList, Integer uid) {
		//排序
		Order order1 = new Order(Direction.DESC, "date");
		Sort sort = new Sort(order1);
		PageRequest pageable = new PageRequest(pageList.getPageNo()- 1, pageList.getPageSize(),sort);
		//将查询条件设置静态变量给匿名类传值
		final Integer uId = uid;
		//通常使用 Specification 的匿名内部类
		Specification<WorkLog> specification = new Specification<WorkLog>() {
			
			@Override
			public Predicate toPredicate(Root<WorkLog> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				//联表条件查询 主表worklog 连接表User
				Join join = root.join(root.getModel().getSingularAttribute("user", User.class),JoinType.INNER);
				Path path = join.get("id");
				Predicate predicate = cb.equal(path, uId);
				return predicate;
			}
		};
		return workLogRepository.findAll(specification, pageable);
	}

	/**
	 * 查询当前用户某个日期的个人日志
	 */
	@Override
	@Transactional(readOnly=true)
	public Page<WorkLog> queryPageList(PageBean<WorkLog> pageList,
			Date date, Integer uid) {
		//排序
		Order order1 = new Order(Direction.DESC, "date");
		Sort sort = new Sort(order1);
		PageRequest pageable = new PageRequest(pageList.getPageNo()- 1, pageList.getPageSize(),sort);
		////将查询条件设置静态变量给匿名类传值
		final Integer uId = uid;
		final Date dt = date;
		//通常使用 Specification 的匿名内部类
		Specification<WorkLog> specification = new Specification<WorkLog>() {
			
			@Override
			public Predicate toPredicate(Root<WorkLog> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				//联表条件查询 主表worklog 连接表User
				Join join = root.join(root.getModel().getSingularAttribute("user", User.class),JoinType.INNER);
				Path path1 = join.get("id");
				Path path2 = root.get("date");
				Predicate predicate1 = cb.equal(path1, uId);
				Predicate predicate2 = cb.equal(path2, dt);
				query.where(predicate1,predicate2);
				return null;
			}
		};
		return workLogRepository.findAll(specification, pageable);
	}

	/**
	 * 添加用户
	 */
	@Override
	public void addWorkLog(WorkLog worklog) {
		workLogRepository.save(worklog);
	}

	/**
	 * 更新日志
	 */
	@Override
	public void updateWorkLog(Integer worklogId, String title, String log,
			Date date) {
		workLogRepository.updateWorkLog(worklogId, title, log,
				date);	
	}
}
